package com.rs.game.player.controlers;

import java.util.ArrayList;

import com.rs.game.RegionBuilder;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;

/*
 * MADE THIS SHIT CODE ON 20minutes for fun, opens doors XD
 */
public class Dungeonnering extends Controler {

	public static enum Room {
		START_ROOM(14,648,2),
		TEST1_ROOM(12,646,2),
		TEST2_ROOM(10,644,2),
		FINALBOSS_ROOM(25, 691, 2);
		protected int regionX, regionY, roomSize;
		
		private Room(int regionX, int regionY, int roomSize) {
			this.regionX = regionX;
			this.regionY = regionY;
			this.roomSize = roomSize;
		}
	}
	
	public static class HandledRoom {
		
		protected int dynamicRegionX;
		protected int dynamicRegionY;
		protected Room room;
		
		public HandledRoom(int dynamicRegionX, int dynamicRegionY, Room room) {
			this.dynamicRegionX = dynamicRegionX;
			this.dynamicRegionY = dynamicRegionY;
			this.room = room;
		}
	}
	int startDungeonRegionX;
	int startDungeonRegionY;
	
	private final ArrayList<HandledRoom> handledRooms = new ArrayList<HandledRoom>();
	
	@Override
	public void start() {
		WorldTile startTile = (WorldTile) this.getParameters()[0];
		startDungeonRegionX = startTile.getRegionX();
		startDungeonRegionY = startTile.getRegionY();
		buildDungeon(-1,  Room.START_ROOM);
		player.setNextWorldTile(new WorldTile(new WorldTile((startDungeonRegionX << 3) + 8, (startDungeonRegionY << 3) + 8, 0), 2));
	}
	
	
	public HandledRoom getMyPlayerRoom() {
		int myRegionX = player.getRegionX();
		int myRegionY = player.getRegionY();
		for(HandledRoom room : handledRooms) {
			if(myRegionX >= room.dynamicRegionX && myRegionX <= room.dynamicRegionX+room.room.roomSize 
				&& myRegionY >= room.dynamicRegionY  && myRegionY <= room.dynamicRegionY+room.room.roomSize)
				return room;
		}
		return null;
	}
	/*
	 * roomsize in region blocks. 1 - 8x8 2 16x16 3 24x24
	 */
	public void buildDungeon(int doorDirection, Room room) {
		System.out.println(doorDirection);
		int[] planes = new int[1]; //just plane 0 has to be added
		if(doorDirection == -1) { //dungeon start
		//	RegionBuilder.copyAllPlanesMap(room.regionX, room.regionY, startDungeonRegionX, startDungeonRegionY, room.roomSize);
		RegionBuilder.copyMap(room.regionX, room.regionY, startDungeonRegionX, startDungeonRegionY, room.roomSize, planes, planes);
		handledRooms.add(new HandledRoom(startDungeonRegionX, startDungeonRegionY, room));
		}else {
			HandledRoom playerRoom = getMyPlayerRoom();
			if(playerRoom == null) {
				return;
			} if(doorDirection == 0) {
				RegionBuilder.copyMap(room.regionX, room.regionY, playerRoom.dynamicRegionX-room.roomSize, playerRoom.dynamicRegionY, room.roomSize, planes, planes);
				handledRooms.add(new HandledRoom(playerRoom.dynamicRegionX-room.roomSize, playerRoom.dynamicRegionY, room));
			} else if (doorDirection == 1) {
				RegionBuilder.copyMap(room.regionX, room.regionY, playerRoom.dynamicRegionX,  playerRoom.dynamicRegionY+playerRoom.room.roomSize, room.roomSize, planes, planes);
				handledRooms.add(new HandledRoom(playerRoom.dynamicRegionX, playerRoom.dynamicRegionY+playerRoom.room.roomSize, room));
			}else if (doorDirection == 2) {
				RegionBuilder.copyMap(room.regionX, room.regionY,  playerRoom.dynamicRegionX+playerRoom.room.roomSize, playerRoom.dynamicRegionY, room.roomSize, planes, planes);
				handledRooms.add(new HandledRoom(playerRoom.dynamicRegionX+room.roomSize, playerRoom.dynamicRegionY, room));
			}else if (doorDirection == 3) {
				RegionBuilder.copyMap(room.regionX, room.regionY, playerRoom.dynamicRegionX, playerRoom.dynamicRegionY-room.roomSize, room.roomSize, planes, planes);
				handledRooms.add(new HandledRoom(playerRoom.dynamicRegionX, playerRoom.dynamicRegionY-room.roomSize, room));
			}
			else
				return;
			player.setForceNextMapLoadRefresh(true);
			player.loadMapRegions();
		}
	}
	
	@Override
	public boolean processObjectClick1(final WorldObject object) {
		if(object.getId() == 50343) {
			buildDungeon(object.getRotation(), Math.random() >= 0.5 ? Room.TEST2_ROOM : Room.TEST1_ROOM);
			return false;
		}
		return true;
	}
	
	public void destroyDungeon() {
		for(HandledRoom room : handledRooms) 
			RegionBuilder.destroyDynamicRegion(((room.dynamicRegionX / 8) << 8) + (room.dynamicRegionY / 8));
	}
	
	@Override
	public boolean logout() {
		destroyDungeon();
		return true; //so remove script
	}
	
	@Override
	public void forceClose() {
		destroyDungeon();
	}

}
